package com.huanying.goodjob.jobtype;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.huanying.framework.BaseController;
import com.huanying.framework.PageBean;
import com.huanying.framework.utils.MemCachedManager;
import com.huanying.goodjob.skill.Skill;
import com.huanying.goodjob.skill.SkillService;

@Controller
public class JobtypeController extends BaseController{
	private static Logger logger = Logger.getLogger(JobtypeController.class);
	@Autowired
	private JobtypeService jobtypeService;
	@Autowired
	private SkillService skillService;
	 private MemCachedManager cache ;
	 private MemCachedManager cache1 ;
	 //查询工种
	@RequestMapping(value = "/show_add_jobtype.do")
	public String show_add_jobtype(String name, String status, String page_num, Model model) {
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		map.put("name", name);
		map.put("status", status);

		if (page_num == null) {
			page_num = "1";
		}
		try {
			pageBean = jobtypeService.search_jobtype(map,  super.getPageSize(), Integer.valueOf(page_num));
		} catch (NumberFormatException e) {
			logger.error("查询工种", e);
			// TODO Auto-generated catch block
			e.getMessage();
		} catch (Exception e) {
			logger.error("查询工种", e);
			// TODO Auto-generated catch block
		}
		model.addAttribute("name", name);
		model.addAttribute("status", status);
		model.addAttribute("jobtypes", pageBean.getList());
		model.addAttribute("page", pageBean);
		model.addAttribute("pageSize", pageBean.getPageSize());

		return "/jobtype/add_jobtype";
	}
//添加工种
	@RequestMapping(value = "/add_jobtype.do")
	@ResponseBody
	public Map<String, String> add_jobtype(@RequestBody Jobtype jobtype, String status) {
		Map<String, String> map = new HashMap<String, String>();
		if (jobtype.getId() == 0) {
			jobtype.setStatus(1);
			try {
				jobtypeService.add_jobtype(jobtype);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				logger.error("添加工种失败", e);
			}
		} else {

			jobtype.setStatus(Integer.parseInt(status));

			try {
				jobtypeService.update_jobtype(jobtype);
				jobtypeService.delete_jobtype_skill(jobtype.getId(), Integer.parseInt(status));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				logger.error("修改工种失败", e);
			}
			
		}
		map.put("flag", "1");
		map.put("id", String.valueOf(jobtype.getId()));
		return map;
	}
//删除工种
	@RequestMapping(value = "/delete_jobtype.do", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> delete_jobtype(int id){
		int flog = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			flog = jobtypeService.delete_jobtype(id);
			jobtypeService.delete_jobtype_skill(id, 0);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("删除工种失败",e);
		}
		
		map.put("flog", flog);
		return map;
	}
//获取单个工种
	@RequestMapping(value = "/get_jobtype.do", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> get_jobtype(int id){
		Map<String, Object> map = new HashMap<String, Object>();
		Jobtype jobtype;
		try {
			jobtype = jobtypeService.getbyId_jobtype(id);
			map.put("jobtype", jobtype);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("获取单个工种失败",e);
		}
		
		
		return map;
	}
	
	
	//前台获取工种
	@RequestMapping(value = "/front_get_jobtypes.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String get_jobtypes(String jsoncallback){
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String,Object> mapcache=new HashMap<String,Object>();
		List list=new ArrayList();
		cache=MemCachedManager.getInstance();
		if(cache.get("jobtype")!=null)
		{
			mapcache=(Map<String,Object>)cache.get("jobtype");
			Iterator<Map.Entry<String, Object>> entries = mapcache.entrySet().iterator();  
			  
			while (entries.hasNext()) {  
			  
				Map.Entry<String, Object> entry =entries.next();  
				if(((Jobtype)entry.getValue()).getStatus()==1)
				{
			    list.add(entry.getValue());
				}
			  
			}  
			  map.put("list",list);
		}else{
		     try {
			       list=jobtypeService.search_jobtypes();
			       map.put("list",list);
		         } catch (Exception e) {
			       logger.error("前台获取工种失败",e);
			// TODO Auto-generated catch block
		         }
		     }
		Gson gson=new Gson();
		String str=gson.toJson(map);
		return jsoncallback+"("+str+")";
		
	}
//前台获取所以的工种和技能	
	@RequestMapping(value = "/front_get_jobtypeandskill.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String get_jobtypeandskill(String jsoncallback){
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String,Object> mapcache=new HashMap<String,Object>();
		Map<String,Object> mapcache1=new HashMap<String,Object>();
		List list=new ArrayList();
		List list1=new ArrayList();
		cache=MemCachedManager.getInstance();
		cache1=MemCachedManager.getInstance();
		if(cache.get("jobtype")!=null&&cache.get("skill")!=null)
		{
			mapcache=(Map<String,Object>)cache.get("jobtype");
			Iterator<Map.Entry<String, Object>> entries = mapcache.entrySet().iterator();  
			  
			while (entries.hasNext()) {  
			  
				Map.Entry<String, Object> entry =entries.next();  
				if(((Jobtype)entry.getValue()).getStatus()==1)
				{
			    list.add(entry.getValue());
				}  
			}
			
			mapcache1=(Map<String,Object>)cache1.get("skill");
			Iterator<Map.Entry<String, Object>> entries1 = mapcache1.entrySet().iterator();  
			  
			while (entries1.hasNext()) {  
			  
				Map.Entry<String, Object> entry1 =entries1.next();  
				if(((Skill)entry1.getValue()).getStatus()==1)
				{
			    list1.add(entry1.getValue());
				}  
			}
			map.put("jobtypelist",list);
			map.put("skilllist",list1);
		}else{
		     try {
			       list=jobtypeService.search_jobtypes();
			       list1=skillService.get_allSkill();
			       map.put("jobtypelist",list);
			       map.put("skilllist", list1);
		         } catch (Exception e) {
			       logger.error("前台获取工种失败",e);
			// TODO Auto-generated catch block
		         }
		     }
		Gson gson=new Gson();
		String str=gson.toJson(map);
		return jsoncallback+"("+str+")";
	}
	
}
